import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class LeetCode {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> tmp = new ArrayDeque<>();
        tmp.add(root);

        while(!tmp.isEmpty()){
            int sz = tmp.size();
            int min = Integer.MIN_VALUE;
            for (int i = 0; i < sz; i++) {
                TreeNode t = tmp.poll();
                min = Math.max(min,t.val);
                if(t.left!=null){
                    tmp.add(t.left);
                }
                if(t.right!=null){
                    tmp.add(t.right);
                }
            }
            ret.add(min);
        }
        return ret;
    }
}
